/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       tcpconnection.h
 * Author:     karooolek
 * Created on: 2009-03-14
 *
 **********************************************************************************************************************/

#ifndef TCPCONNECTION_H_
#define TCPCONNECTION_H_

#include "ipconnection.h"

namespace mGameEngine
{
namespace Network
{

/**
 * TCP/IP connection.
 * Network connection with another machine over TCP/IP stream protocol.
 * Connection is established between exactly two machines.
 * When server opens connection, it must wait until client connects.
 * Using TCP/IP protocol guarantees 100% of correct data transmission
 * unless connection is lost.
 */
class TCPConnection : public IPConnection
{
public:
    /**
     * Create new TCP/IPconnection.
     */
    TCPConnection();

    /**
     * Create new TCP/IP connection with specified address.
     * @param ipaddr IP address.
     */
    TCPConnection(const IPAddress &ipaddr);

    /**
     * Destroy TCP/IP connection.
     */
    ~TCPConnection();

    /**
     * Open TCP/IP connection and wait for client.
     */
    void open();

    /**
     * Close TCP/IP connection.
     */
    void close();

    /**
     * Connect to TCP/IP connection server.
     */
    void connect();

    /**
     * Disconnect from TCP/IP connection server.
     */
    void disconnect();

    /**
     * Send data to another machine using TCP/IP protocol
     * @param data data to be sent.
     * @param size data size.
     * @return number of sent bytes os 0 if connection was lost.
     */
    virtual uint send(const uint8 *data, uint size);

    /**
     * Receive data from another machine.
     * @param buffer buffer to store received data.
     * @param size buffer size.
     * @return number of received bytes or 0 if connection was lost.
     */
    virtual uint receive(uint8 *buffer, uint size);
};

}
}

#endif // TCPCONNECTION_H_
